package com.cos.edoc.web.controller;

import com.cos.edoc.entity.EdocEntry;
import com.cos.edoc.services.EdocEntryServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author: COS
 * @time: 2022/3/14 15:18
 * @description:
 */
@Controller
@RequestMapping("/entry")
public class EdocEntryController {
    @Autowired
    private EdocEntryServices entryServices;
    @RequestMapping("/getBooksBycategoryId")
    public String getBooksBycategoryId(Integer categoryId, Model model){
        //访问业务层
        List<EdocEntry> entryBooks =  entryServices.getBooksBycategoryId(categoryId);
        model.addAttribute("books", entryBooks);
        return "index";
    }
    @RequestMapping("/getBooksBycategoryId2")
    @ResponseBody
    public List<EdocEntry> getBooksBycategoryId2(Integer categoryId){
        System.out.println("进入二号方法");
        //访问业务层
        return entryServices.getBooksBycategoryId(categoryId);
    }

    @RequestMapping("/del/{id}")
    @ResponseBody
    public String del(@PathVariable Integer id){
        if(entryServices.del(id)){
            return "1";//成功
        }
        return "0";//失败
    }

    @RequestMapping("/add")
    @ResponseBody
    public String add(EdocEntry entry){
        System.out.println(entry);
        boolean ans = entryServices.add(entry);
        if (ans) {
            return "1";
        }
        return "0";
    }
    @RequestMapping("/update")
    @ResponseBody
    public String update(EdocEntry entry){
        System.out.println(entry);
        boolean ans = entryServices.update(entry);
        if (ans) {
            return "1";
        }
        return "0";
    }


    @RequestMapping("/getBookById")
    public String getBookById(Integer id,Model model){
       EdocEntry e =  entryServices.getBookById(id);
       model.addAttribute("e",e);
       return "updateBooks";
    }
}
